package com.atung.provider;

import java.util.Objects;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import com.atung.model.BusOrderCar;

public class BusOrderCarProvider {

	public String saveBusOrderCarProvider(BusOrderCar busOrderCar ){
		
		return new SQL(){
			{
				INSERT_INTO("bus_order_car");
				VALUES("id", "#{id}");
				VALUES("payment_id", "#{paymentId}");
				VALUES("open_id", "#{openId}");
				VALUES("order_state", "#{orderState}");
				VALUES("allocation_id", "#{allocationId}");				
				VALUES("apply_number", "#{applyNumber}");
				if(Objects.nonNull(busOrderCar.getIsDelect())){
					VALUES("is_delect", "#{isDelect}");
				}
				VALUES("creation_time", "#{creationTime}");
				VALUES("modification_time", "#{modificationTime}");	
			}
		}.toString();
	}
	/**
	 * 获取订单列表
	 * @param openId
	 * @param orderState
	 * @return
	 */
	public String getOrderListProvider(@Param("openId")String openId,@Param("orderState")Integer orderState){
		
		StringBuffer sql=new StringBuffer();
		sql.append("select boc.id orderId,boc.order_state,brp.departl_name,brp.destination ,brp.start_data from bus_order_car  boc inner join bus_school_bus_payment bsbp on boc.payment_id=bsbp.id "
				+ "inner join bus_rout_publish brp on bsbp.rout_publish_id=brp.id ");
		sql.append(" where  boc.open_id=#{openId} and boc.is_delect=0 ");
		if(Objects.nonNull(orderState)){
			sql.append(" and  boc.order_state=#{orderState} ");
		}
		sql.append(" order by  boc.order_state desc ");
		return sql.toString();
	}
	
	/**
	 * 获取订单详情
	 * @param orderId
	 * @return
	 */
	public String orderDetailsProvider(@Param("orderId") Long orderId){
	
		StringBuffer sql=new StringBuffer();
		sql.append("select boc.id orderId,boc.order_state,brp.departl_name,brp.destination ,boc.allocation_id from bus_order_car  boc inner join bus_school_bus_payment bsbp on boc.payment_id=bsbp.id "
				+ "inner join bus_rout_publish brp on bsbp.rout_publish_id=brp.id ");
		sql.append(" where  boc.id=#{orderId} and boc.is_delect=0 ");
		return sql.toString();
	}
}
